﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 坦克大战正式.Properties;

namespace 坦克大战正式
{
    class MyTank:Movething
    {
        public bool IsMoving { get; set; }
        public int HP;
        public int originalX;
        public int originalY;
        public MyTank(int x,int y,int speed)
        {
            IsMoving = false;
            this.X = x;
            this.Y = y;
            originalX = x;
            originalY = y;
            this.Speed = speed;
            BitmapDown = Resources.MyTankDown;
            BitmapLeft = Resources.MyTankLeft;
            BitmapRight = Resources.MyTankRight;
            BitmapUp = Resources.MyTankUp;
            this.Dir = Direction.Up;
            HP = 4;
        }
        public void KeyDown(KeyEventArgs args)
        {
            switch(args.KeyCode)
            {
                case Keys.W:
                    Dir = Direction.Up;
                    IsMoving = true;
                    break;
                case Keys.S:
                    Dir = Direction.Down;
                    IsMoving = true;
                    break;
                case Keys.A:
                    Dir = Direction.Left;
                    IsMoving = true;
                    break;
                case Keys.D:
                    Dir = Direction.Right;
                    IsMoving = true;
                    break;
                case Keys.Space:
                    Attack();
                    break;
            }
        }
        private void Attack()
        {
            SoundManger.PlayFire();
            int x = this.X;
            int y = this.Y;
            switch (Dir)
            {
                case Direction.Up:
                    x = x + Width / 2;
                    break;
                case Direction.Down:
                    x = x + Width / 2;
                    y = y + Height;
                    break;
                case Direction.Left:
                    y = y + Height / 2;
                    break;
                case Direction.Right:
                    x = x + Width;
                    y = y + Height / 2;
                    break;

            }
            //GameObjectManager.CreateBullet(x-Width/2, y-Height/2, Tag.MyTank, Dir);
            GameObjectManager.CreateBullet(x, y, Tag.MyTank, Dir);

        }
        public override void Update()
        {
            MoveCheck();//移动检查
            Move();

            base.Update();
        }
        private void MoveCheck()
        {
            #region 检查有没有超出窗体边界
            if (Dir == Direction.Up)
            {
                if (Y - Speed < 0)
                {
                    IsMoving = false; return;
                }
            }
            else if (Dir == Direction.Down)
            {
                if (Y + Speed > 450 - Height)
                {
                    IsMoving = false; return;
                }
            }
            else if (Dir == Direction.Left)
            {
                if (X - Speed < 0)
                {
                    IsMoving = false; return;
                }
            }
            else if (Dir == Direction.Right)
            {
                if (X + Speed > 450 - Width)
                {
                    IsMoving = false; return;
                }
            }
            #endregion

            //检查有没有和其它元素发生碰撞
            Rectangle rect = GetRectangle();
            switch (Dir)
            {
                case Direction.Up:
                    rect.Y -= Speed;
                    break;
                case Direction.Down:
                    rect.Y += Speed;
                    break;
                case Direction.Right:
                    rect.X += Speed;
                    break;
                case Direction.Left:
                    rect.X -= Speed;
                    break;
            }
            if (GameObjectManager.IsCollideWall(rect) != null)
            {
                IsMoving = false; return;
            }
            if (GameObjectManager.IsCollideSteel(rect) != null)
            {
                IsMoving = false; return;
            }
            if (GameObjectManager.IsCollideBoss(rect))
            {
                IsMoving = false; return;
            }
        }
        public void Move()
        {
            if (IsMoving == false) return;
            switch (Dir)
            {
                case Direction.Up:
                    Y -= Speed;
                    break;
                case Direction.Down:
                    Y += Speed;
                    break;
                case Direction.Left:
                    X -= Speed;
                    break;
                case Direction.Right:
                    X += Speed;
                    break;
            }
        }
        public void KeyUp(KeyEventArgs args)
        {
            switch (args.KeyCode)
            {
                case Keys.W:
                    IsMoving = false;
                    break;
                case Keys.S:
                    IsMoving = false;
                    break;
                case Keys.A:
                    IsMoving = false;
                    break;
                case Keys.D:
                    IsMoving = false;
                    break;
            }
        }
        public void TakeDamage()
        {
            HP--;
            if(HP<=0)
            {
                X = originalX;
                Y = originalY;
                HP = 4;
            }
        }
    }
}
